package view;

import document.Context;
import document.HtmlDocument;
import search.LeafIndexer;
import treeview.NodeView;

import javax.swing.*;
import java.awt.*;

/**
 * Created by Pavel Nikolaev
 */
public class HtmlPanel extends JPanel implements Scrollable {
    private HtmlDocument myDocument;
    private NodeView myView;
    private LeafIndexer myLeafViewIndexer;
    private boolean isViewLocationSet;
    private Context myContext;

    public HtmlPanel() {
        myDocument = null;
        myView = null;
        isViewLocationSet = false;
        myContext = new Context();
        myLeafViewIndexer = null;
        setBackground(myContext.getBackgroundColor());
    }

    public void setDocument(HtmlDocument newDoc) {
        if (newDoc == null) return;
        HtmlDocument oldDoc = myDocument;
        myDocument = newDoc;
        myView = null;
        this.setSize(0, 0);
        myView = myDocument.createView(myContext);
        myLeafViewIndexer = new LeafIndexer(myView);
        this.firePropertyChange("LeafViewIndexerChanged", null, myLeafViewIndexer);
        this.invalidate();
        this.repaint();
        this.firePropertyChange("DocumentUpdated", oldDoc, newDoc);
    }

    public NodeView getView() {
        return myView;
    }

    public LeafIndexer getIndexer() {
        return myLeafViewIndexer;
    }

    @Override
    public void setSize(int width, int height) {
        if (myView == null) {
            super.setSize(width, height);
        } else {
            if (myView.getWidth() != width) {
                Graphics g = getGraphics();
                myView.fitIntoWidth(g, width);
                // TODO think about way don't make update of all leaves
                myLeafViewIndexer.update(myView);
                this.firePropertyChange("LeafViewIndexerChanged", null, myLeafViewIndexer);
                isViewLocationSet = false;
            }
            if (myView.getHeight() < height) myView.setHeight(height);

            super.setSize(myView.getWidth(), myView.getHeight());
        }
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        if (myView == null) return;

        if (!isViewLocationSet) {
            Point p = g.getClipBounds().getLocation();
            myView.drawAt(g, p.x, p.y);
            isViewLocationSet = true;
        } else {
            myView.paint(g);
        }
    }

    public Dimension getPreferredScrollableViewportSize() {
        if (myView != null) {
            return new Dimension(myView.getWidth(), myView.getHeight());
        } else {
            return new Dimension();
        }
    }



    public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
        return 10;
    }

    public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
        return 100;
    }

    public boolean getScrollableTracksViewportWidth() {
        return false;
    }

    public boolean getScrollableTracksViewportHeight() {
        return false;
    }
}
